package com.winit.openapi.wh.allocation.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.allocation.command.order.AllocationRecommendCommand;
import com.winit.oms.spi.allocation.service.fee.AllocationOrderFeeCalculateService;
import com.winit.oms.spi.allocation.service.order.AllocationOrderService;
import com.winit.oms.spi.allocation.vo.AllocationOrderEnumContants.DocumentType;
import com.winit.oms.spi.allocation.vo.order.AllocationOrderVo;
import com.winit.oms.spi.vo.v2.response.AllocationOrderSubmitResultOrderVo;
import com.winit.oms.spi.vo.v2.response.OrderSubmitResultVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.wh.allocation.builder.AllocationOrderBuilder;
import com.winit.openapi.wh.allocation.vaildator.AllocationOrderfieldValidator;

@Controller 
@RequestMapping(value = "/wh/recommendPlan")
public class AllocationRecommendPlanController extends BaseController {
	
	@Resource
	private AllocationOrderFeeCalculateService allocationOrderFeeCalculateService;
    @Resource
    private AllocationOrderService allocationOrderService;
    
	@RequestMapping(value="/feeEstimate")
	public String feeEstimate(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
		// TODO 一批一批预估费用  失败了怎么办？
		Object obj = requestMsg.getData(); 
		AllocationRecommendCommand command = new AllocationRecommendCommand();
		if (obj instanceof JSONObject) {
			JSONObject jsonObj = (JSONObject) obj;
			AllocationOrderfieldValidator.validRecommendAllocation(jsonObj);
			command = AllocationOrderBuilder.buildAllocationOrderListCommand(jsonObj);
	   }else{
		   throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
	   }
		AllocationOrderSubmitResultOrderVo vos = allocationOrderService 
				.saveFirstStepBatch(command);
		if (null != vos) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("feeEstimateList", 
					AllocationOrderBuilder.buildResultMap(vos.getOrderList()));
			resultMap.put("usableBalance", vos.getUsableBalance());
			resultMap.put(
					"estimatedFreezingAmount",
					vos.getEstimatedFreezingAmount() == null ? "预估费用失败" : vos
							.getEstimatedFreezingAmount());
			resultMap.put("estimatedVolume", vos.getVolume());
			resultMap.put("merchandiseQty", vos.getMerchandiseQty());
			resultMap.put("itemQty", vos.getItemQty());
			responseMsg.setData(resultMap);
		}
		responseMsg.setMsg("SUCCESS");
		return SUCCESS;
	}
	

	@RequestMapping(value = "/submitOrder")
	public String submitOrder(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		Object obj = requestMsg.getData(); 
		AllocationRecommendCommand command = new AllocationRecommendCommand();
		if (obj instanceof JSONObject) {
			JSONObject jsonObj = (JSONObject) obj;
			AllocationOrderfieldValidator.validRecommendAllocation(jsonObj);
			command = AllocationOrderBuilder.buildAllocationOrderListCommand(jsonObj);
	   }else{
		   throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
	   }
		OrderSubmitResultVo resultVo= allocationOrderService.saveSecondStepBatch(command);
		if(resultVo!=null){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("isSuccess", resultVo.getIsSuccess());
            map.put("orderResultList", AllocationOrderBuilder.buildResultList(resultVo.getOrderResultList()));
            responseMsg.setData(map);
        }
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
	}
}